﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using LayerDTO;

namespace LayerDAO
{
    public class MatHangDAO
    {
        public static List<MatHangDTO> LayDanhSachMatHang(string connect)
        {
            DataTable dt = new DataTable();
            DataProvider _provider = new DataProvider(connect);
            if (_provider.OpenConnect())
                dt = _provider.SqlExcuteQuery("LayDanhSachMatHang", null);
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            List<MatHangDTO> list = new List<MatHangDTO>();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    MatHangDTO matHang = new MatHangDTO();
                    matHang.MaMatHang = dt.Rows[i]["MMH"].ToString();
                    matHang.TenMatHang= dt.Rows[i]["TenMH"].ToString();
                    matHang.SoLuongTon = int.Parse(dt.Rows[i]["SoLuongTon"].ToString());
                    matHang.MaDV = dt.Rows[i]["MDV"].ToString();
                    matHang.MaLoaiMH = dt.Rows[i]["MLMH"].ToString();
                    matHang.DaXoa = bool.Parse(dt.Rows[i]["DaXoa"].ToString());
                    list.Add(matHang);
                }
            }
            return list;
        }
        public static List<MatHangDTO> TimMatHang(string connect,string tenMatHang)
        {
            DataTable dt = new DataTable();
            DataProvider _provider = new DataProvider(connect);
            if (_provider.OpenConnect())
                dt = _provider.SqlExcuteQuery("TimMatHangTheoTenMatHang", new SqlParameter("@tenMatHang", tenMatHang));
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            List<MatHangDTO> list = new List<MatHangDTO>();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    MatHangDTO matHang = new MatHangDTO();
                    matHang.MaMatHang = dt.Rows[i]["MMH"].ToString();
                    matHang.TenMatHang = dt.Rows[i]["TenMH"].ToString();
                    matHang.SoLuongTon = int.Parse(dt.Rows[i]["SoLuongTon"].ToString());
                    matHang.MaDV = dt.Rows[i]["MDV"].ToString();
                    matHang.MaLoaiMH = dt.Rows[i]["MLMH"].ToString();
                    matHang.DaXoa = bool.Parse(dt.Rows[i]["DaXoa"].ToString());
                    list.Add(matHang);
                }
            }
            return list;
        }
        public string ThemMatHang(MatHangDTO matHang,string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("ThemMatHangMoi", new SqlParameter("@tenMH", matHang.TenMatHang),
                                                             new SqlParameter("@soLuong", matHang.SoLuongTon),
                                                             new SqlParameter("@donVi", matHang.MaDV),
                                                             new SqlParameter("@Gia", matHang.Gia),
                                                             new SqlParameter("@maLoai", matHang.MaLoaiMH), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }

        public string SuaMatHang(MatHangDTO matHang, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("SuaMatHang", new SqlParameter("@maMH", matHang.MaMatHang),
                                                              new SqlParameter("@tenMH", matHang.TenMatHang),
                                                             new SqlParameter("@soLuong", matHang.SoLuongTon),
                                                             new SqlParameter("@donVi", matHang.MaDV),
                                                             new SqlParameter("@Gia", matHang.Gia),
                                                             new SqlParameter("@maLoai", matHang.MaLoaiMH),
                                                              new SqlParameter("@daXoa", matHang.DaXoa), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
        public string XoaMatHang(MatHangDTO matHang,string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("XoaMatHang", new SqlParameter("@maMH", matHang.MaMatHang), par);


            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
    }
}